package com.example.medicine.controller;


import com.example.medicine.Bean.yonghu;
import com.example.medicine.Mapper.yonghuMapper;
import com.example.medicine.md5_guid.md5_guid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import java.util.UUID;
@Slf4j
@Controller
public class RegisterController {
    @Autowired
    private yonghuMapper yonghuMapper;
    /**
     * 上传的文件位置
     */
    private static final String UPLOADED_FOLDER = "D:/FileRecv/uploadImages/";

    /**
     * 显示登录界面
     * @return
     */
    @RequestMapping("/User/register")
    public String registerShow(){
        log.info("registerShow注册界面");
        return "pages/User/register";
    }

    /**
     * 接收注册请求，插入用户信息
     * @param username
     * @param password
     * @param image
     * @param type
     * @param tel
     * @param email
     * @param company
     * @param addr
     * @return
     */
    @PostMapping(value = "/register")
    public String register(
            @RequestParam("userid") String userId,
                           @RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("image") MultipartFile image,
                           @RequestParam("type") String type,
                           @RequestParam("tel") String tel,
                           @RequestParam("email") String email,
                           @RequestParam("company") String company,
                           @RequestParam("addr") String addr,
                           Model model
            ) throws IOException{
        log.info("register用户注册");
        // 创建用户对象
        int checkCode = 3;
        yonghu tempYonghu=yonghuMapper.getPasswordByUserid(userId);
        if(tempYonghu!=null&&tempYonghu.getPassword()!=null){//判断用户名是否已被使用
            model.addAttribute("message",checkCode);
            return "pages/User/register";
        }
        yonghu user = new yonghu();
        user.setUserid(userId);
       log.info(user.getUserid());
        user.setUsername(username);
        user.setPassword(new md5_guid(password).md5());
        user.setType(type);
        user.setTel(tel);
        user.setEmail(email);
        user.setCompany(company);
        user.setAddr(addr);
        String imageName=null;
        if (image != null && !image.isEmpty()) {
            // 处理上传的图片文件
            String fname = image.getOriginalFilename();    //获取原始的文件名
            //用UUID作为新文件名，扩展名不变  避免同名覆盖
            String newName = UUID.randomUUID()+ "." + fname.substring(fname.lastIndexOf(".")+1);
            File dest_file = new File( UPLOADED_FOLDER + newName );
            image.transferTo(dest_file);
            imageName=newName;
        }
        user.setImage(imageName);
        yonghuMapper.insert(user);
        checkCode = 2;
        model.addAttribute("message",checkCode);
        return "pages/login";  // 重定向到登录页面
    }
}
